home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Snippets / Networking / testNBP / testNBP.c next >
Encoding:
C/C++ Source or Header  |  1994-11-10  |  5.8 KB  |  197 lines  |  [TEXT/MPS ]

  1. #include <Types.h>
  2. #include <OSUtils.h>
  3. #include <Appletalk.h>
  4. #include <Memory.h>
  5. #include <Strings.h>
  6. #include <String.h>
  7. #include <stdio.h>
  8. #include <Events.h>
  9. #include <StdLib.h>
  10.  
  11.  
  12. OSErr                result;
  13. ATPParamBlock        my_atp;
  14. MPPParamBlock        my_nbp,self;
  15. NamesTableEntry        entity1, entity2, entity3;
  16. char                tempBuffer[2048];
  17. short                input;
  18. char                *nbpObject, *nbpType, *nbpZone;
  19. char                newSocket;
  20. short                tempNode;
  21. char                saveSelfState;
  22.  
  23. main()
  24. {
  25.     if (MPPOpen() != noErr)    {
  26.         printf("Error opening MPP - Aborting program!\n");
  27.         fflush(stdout);
  28.         exit(-1);
  29.     }
  30.     
  31.     if (ATPLoad() != noErr) {
  32.         printf("Error opening ATP - Aborting program!\n");
  33.         fflush(stdout);
  34.         exit(-1);
  35.     }
  36.     
  37. /* open a socket */
  38.     my_atp.ATP.atpSocket = 0;
  39.     my_atp.ATP.addrBlock.aNet = 0;
  40.     my_atp.ATP.addrBlock.aNode = 0;
  41.     my_atp.ATP.addrBlock.aSocket = 0;
  42.     result = POpenATPSkt(&my_atp, false);
  43.     printf("Result of opening socket was %d.\n", result);
  44.     printf("atpSocket is %u.\n", (unsigned char) my_atp.ATP.atpSocket);
  45.     
  46. /* Get the current node address info for PConfirmName */
  47.     GetNodeAddress(&tempNode, (short *) &my_atp.ATP.addrBlock.aNet);
  48.     my_atp.ATP.addrBlock.aNode = tempNode;
  49.     my_atp.ATP.addrBlock.aSocket = my_atp.ATP.atpSocket;
  50.     
  51.     printf("addrBlock.aNet is %d.\n", my_atp.ATP.addrBlock.aNet);
  52.     printf("addrBlock.aNode is %d.\n", my_atp.ATP.addrBlock.aNode);
  53.     printf("addrBlock.aSocket is %d.\n\n", my_atp.ATP.addrBlock.aSocket);
  54.     fflush(stdout);
  55.         
  56.     if (result != noErr)    {
  57.     
  58.         printf("Error opening socket - Aborting program!\n");
  59.         fflush(stdout);
  60.         exit(-1);
  61.     }
  62.     
  63. /* set selfsend so that we can see ourselves */
  64.     printf("Setting Self Send state to true.\n");
  65.     self.SETSELF.newSelfFlag = 1;
  66.     PSetSelfSend(&self, false);
  67.     saveSelfState = self.SETSELF.oldSelfFlag;
  68.     
  69.     if (saveSelfState)
  70.         printf("Old Self send state was true.\n");
  71.     else
  72.         printf("Old Self send state was false.\n\n");
  73.         
  74.     
  75.  
  76. /* register first item with upper case initial letters */
  77.     nbpObject = "\pa";
  78.     nbpType = "\pComputer Server";
  79.     nbpZone = "\p*";
  80.     
  81.     
  82.     NBPSetNTE ((Ptr) &entity1, (Ptr)nbpObject, (Ptr)nbpType, (Ptr)nbpZone, my_atp.ATP.atpSocket);
  83.     my_nbp.NBP.NBPPtrs.ntQElPtr = (Ptr) &entity1;
  84.     my_nbp.NBP.interval = 4;
  85.     my_nbp.NBP.count = 2;
  86.     my_nbp.NBP.parm.verifyFlag = true;
  87.  
  88.     result = PRegisterName(&my_nbp, false);
  89.  
  90.     printf("Result of first register was %d.\n", result);
  91.     printf("ioResult returned %d.\n", my_nbp.NBP.ioResult);
  92.     
  93.     fflush(stdout);
  94.  
  95. /* Confirm first name */
  96.     NBPSetEntity ((Ptr) &entity1.nt.entityData[0], (Ptr)nbpObject, (Ptr)nbpType, (Ptr)nbpZone);
  97.     my_nbp.NBP.NBPPtrs.entityPtr = (Ptr) &entity1.nt.entityData[0];
  98.     my_nbp.NBP.interval = 4;
  99.     my_nbp.NBP.count = 3;
  100.     BlockMove ((Ptr) &my_atp.ATP.addrBlock, (Ptr) &my_nbp.NBP.parm.Confirm.confirmAddr, sizeof(AddrBlock));
  101.  
  102.     result = PConfirmName(&my_nbp, false);
  103.  
  104.     printf("Result of first confirm was %d.\n", result);
  105.     printf("ioResult returned %d.\n\n", my_nbp.NBP.ioResult);
  106.     fflush(stdout);
  107.  
  108. /* Register second name with lower case initial letters */    
  109.     nbpObject = "\pb";
  110.     nbpType = "\pcomputer server";
  111.  
  112.     NBPSetNTE ((Ptr) &entity2, (Ptr)nbpObject, (Ptr)nbpType, (Ptr)nbpZone, my_atp.ATP.atpSocket);
  113.     my_nbp.NBP.NBPPtrs.ntQElPtr = (Ptr) &entity2;
  114.     my_nbp.NBP.interval = 8;
  115.     my_nbp.NBP.count = 3;
  116.     my_nbp.NBP.parm.verifyFlag = true;
  117.  
  118.     result = PRegisterName(&my_nbp, false);
  119.  
  120.     printf("Result of second register was %d.\n", result);
  121.     printf("ioResult returned %d.\n", my_nbp.NBP.ioResult);
  122.     fflush(stdout);
  123.  
  124. /* Confirm second name */
  125.     NBPSetEntity ((Ptr) &entity2.nt.entityData[0], (Ptr)nbpObject, (Ptr)nbpType, (Ptr)nbpZone);
  126.     my_nbp.NBP.NBPPtrs.entityPtr = (Ptr) &entity2.nt.entityData[0];
  127.     my_nbp.NBP.interval = 8;
  128.     my_nbp.NBP.count = 3;
  129.     BlockMove ((Ptr) &my_atp.ATP.addrBlock, (Ptr) &my_nbp.NBP.parm.Confirm.confirmAddr, sizeof(AddrBlock));
  130.  
  131.     result = PConfirmName(&my_nbp, false);
  132.  
  133.     printf("Result of second confirm was %d.\n", result);
  134.     printf("ioResult returned %d.\n\n", my_nbp.NBP.ioResult);
  135.     fflush(stdout);
  136.  
  137.  
  138. /* lookup with lower case name */
  139.     nbpObject = "\p=";
  140. /*    nbpObject = "\p≈"; */
  141.     nbpType = "\pcomputer server";
  142.     
  143.     NBPSetEntity ((Ptr) &entity3.nt.entityData[0], (Ptr)nbpObject, (Ptr)nbpType, (Ptr)nbpZone);
  144.     my_nbp.NBP.NBPPtrs.entityPtr = (Ptr) &entity3.nt.entityData[0];
  145.     my_nbp.NBP.interval = 4;
  146.     my_nbp.NBP.count = 3;
  147.     my_nbp.NBP.parm.Lookup.retBuffPtr = &tempBuffer;
  148.     my_nbp.NBP.parm.Lookup.retBuffSize = 2048;
  149.     my_nbp.NBP.parm.Lookup.maxToGet = 20;
  150.  
  151.     result = PLookupName(&my_nbp, false);
  152.  
  153.     printf("Result of lower case lookup was %d.\n", result);
  154.     printf("ioResult was %d.\n", my_nbp.NBP.ioResult);
  155.     printf("NumGotten was %d.\n\n", my_nbp.NBP.parm.Lookup.numGotten);
  156.     fflush(stdout);
  157.     
  158. /* lookup with upper case name */
  159.     nbpType = "\pComputer Server";
  160.     
  161.     NBPSetEntity ((Ptr) &entity3.nt.entityData[0], (Ptr)nbpObject, (Ptr)nbpType, (Ptr)nbpZone);
  162.     my_nbp.NBP.NBPPtrs.entityPtr = (Ptr) &entity3.nt.entityData[0];
  163.     my_nbp.NBP.interval = 4;
  164.     my_nbp.NBP.count = 3;
  165.     my_nbp.NBP.parm.Lookup.retBuffPtr = &tempBuffer;
  166.     my_nbp.NBP.parm.Lookup.retBuffSize = 2048;
  167.     my_nbp.NBP.parm.Lookup.maxToGet = 20;
  168.  
  169.     result = PLookupName(&my_nbp, false);
  170.     
  171.     printf("Result of upper case lookup was %d.\n", result);
  172.     printf("ioResult was %d.\n", my_nbp.NBP.ioResult);
  173.     printf("NumGotten was %d.\n\n", my_nbp.NBP.parm.Lookup.numGotten);
  174.     fflush(stdout);
  175.  
  176. /* Restore self send state */    
  177.     self.SETSELF.newSelfFlag = saveSelfState;
  178.     PSetSelfSend(&self, false);
  179.     saveSelfState = self.SETSELF.oldSelfFlag;
  180.  
  181.     
  182. /* Remove names */
  183.     my_nbp.NBP.NBPPtrs.entityPtr = (Ptr) &entity1.nt.entityData[0];
  184.     result = PRemoveName(&my_nbp, false);
  185.     printf("Result of removing entity1 was %d.\n", result);
  186.  
  187.     my_nbp.NBP.NBPPtrs.entityPtr = (Ptr) &entity2.nt.entityData[0];
  188.  
  189.     result = PRemoveName(&my_nbp, false);
  190.     printf("Result of removing entity2 was %d.\n", result);
  191.     
  192.     result = PCloseATPSkt(&my_atp, false);
  193.     printf("Result of closing socket was %d.\n", result);
  194.     fflush(stdout);
  195.     ATPUnload();
  196. }
  197.